﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 角色权限服务
    /// </summary>
    public class RoleFunctionService : IRoleFunctionService
    {
        private readonly DbContext dal = new DbContext();

        public bool Add(List<T_Role_Function> functions)
        {
            return dal.Insert(functions);
        }

        public bool Delete(List<string> ids)
        {
            return dal.Deleteable<T_Role_Function>().In(ids).ExecuteCommand() > 0;
        }

        public bool Delete(string companyId, string roleId)
        {
            return dal.Deleteable<T_Role_Function>().Where(p => p.CompanyID == companyId && p.RoleID == roleId).ExecuteCommand() > 0;
        }

        public T_Role_Function GetById(string id)
        {
            return dal.Get<T_Role_Function>(id);
        }

        public List<T_Role_Function> GetList(string companyId, string roleId)
        {
            Expression<Func<T_Role_Function, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyId, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(roleId))
            {
                whereExp = whereExp.Compose((a) => a.RoleID == roleId, Expression.AndAlso);
            }
            return dal.Queryable<T_Role_Function>().Where(whereExp).ToList();
        }

        public List<RoleFunctionVo> GetListByRoleId(string companyId, string roleId)
        {
            Expression<Func<T_Function, T_Role_Function, bool>> whereExp = (a, b) => a.DataStatus == DataStatusEnum.Valid && a.CompanyID == companyId;
            var list = dal.Queryable<T_Function, T_Role_Function>((a, b) => new object[]
            {
                JoinType.Left,a.ID == b.FunctionID && b.RoleID == roleId
            })
            .Where(whereExp)
            .OrderBy((a, b) => a.FunctionIndex, OrderByType.Asc)
            .Select((a, b) => new RoleFunctionVo()
            {
                ID = a.ID,
                PID = a.ParentID,
                Name = a.FunctionName,
                Checked = SqlFunc.IsNullOrEmpty(b.ID) ? false : true,
                Open = true
            })
            .ToList();
            return list;
        }

        public PageData<T_Role_Function> Query(string companyId, string roleId, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Role_Function, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyId, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(roleId))
            {
                whereExp = whereExp.Compose((a) => a.RoleID == roleId, Expression.AndAlso);
            }

            List<T_Role_Function> list = dal.Queryable<T_Role_Function>().Where(whereExp).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Role_Function> { Data = list, Page = page };
        }

        public bool Update(T_Role_Function model)
        {
            return dal.Modify(model);
        }

        public bool Update(string roleId, string companyId, List<string> functionIds)
        {
            try
            {
                dal.BeginTran();
                dal.Deleteable<T_Role_Function>().Where(p => p.CompanyID == companyId && p.RoleID == roleId).ExecuteCommand();
                
                if (functionIds.Count > 0)
                {
                    var roleFunctionList = functionIds.Select(p => new T_Role_Function
                    {
                        ID = Guid.NewGuid().ToString(),
                        RoleID = roleId,
                        CompanyID = companyId,
                        FunctionID = p
                    }).ToList(); ;

                    bool result = dal.Insert<T_Role_Function>(roleFunctionList);
                    if (result == false)
                    {
                        dal.RollbackTran();
                        return false;
                    }
                }
                dal.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                dal.RollbackTran();
                throw ex;
            }
        }
    }
}
